<?php
require_once '../application/library/db/gcDaoFactory.php';
require_once '../application/constant/daoConfig.php';

/**
* 商品新增・编辑画面用的Service
* 商品管理
* @version   0.1
* @author    Suda
*/
class goods_services_goodsAddEditService {
    private $objDaoFactory;

    public function __construct() {
        $this->objDaoFactory = gcDaoFactory::getInstance();
    }

    public function pbGetGoodsInfo($strGoodsNo) {
        $objGoodsDao = $this->objDaoFactory->createDao(daoConfig::T_EC_M_GOODS_DAO);
        $arrGoodsInfo = $objGoodsDao->pbGetGoodsInfo($strGoodsNo);
        return $arrGoodsInfo;
    }

    public function pbGetSupplierInfo($strGoodsNo) {
        $objGoodsDao = $this->objDaoFactory->createDao(daoConfig::T_EC_M_GOODS_REALATED_SUPPLIER_DAO);
        $arrSupplierInfo = $objGoodsDao->pbGetSupplierInfo($strGoodsNo);
        return $arrSupplierInfo;
    }

    public function pbGoodsInsert($arrParameter) {
        $objGoodsDao = $this->objDaoFactory->createDao(daoConfig::T_EC_M_GOODS_DAO);
        $strGoodsNo = $objGoodsDao->pbGoodsInsert($arrParameter);
        if($strGoodsNo != "0") {
            $objGoodsRealatedDao = $this->objDaoFactory->createDao(daoConfig::T_EC_M_GOODS_REALATED_CATG_DAO);
            $intInsertRe = $objGoodsRealatedDao->pbGoodsRealatedInsert($arrParameter,$strGoodsNo);
            $arrReturn = array("InsertRe" => $intInsertRe,"GoodsNo" =>$strGoodsNo);
            return $arrReturn;
        } else {
            return "0";
        }
    }

    public function pbGoodsUpdate($arrParameter) {
        $objGoodsDao = $this->objDaoFactory->createDao(daoConfig::T_EC_M_GOODS_DAO);
        $strUpdateRe = $objGoodsDao->pbGoodsUpdate($arrParameter);
        $objGoodsRealatedDao = $this->objDaoFactory->createDao(daoConfig::T_EC_M_GOODS_REALATED_CATG_DAO);
        $objGoodsRealatedDao->pbDeleteCatgRealated($arrParameter["GoodsNo"]);
        $objGoodsRealatedDao->pbGoodsRealatedInsert($arrParameter,$arrParameter["GoodsNo"]);
        return $strUpdateRe;
    }

    public function pbGoodsRealatedSupplierUpdate($arrSupplierInfo, $strGoodsNo) {
        $objGoodsRealatedSupplierDao = $this->objDaoFactory->createDao(daoConfig::T_EC_M_GOODS_REALATED_SUPPLIER_DAO);
        $objGoodsRealatedSupplierDao->pbGoodsRealatedSupplierDelete($strGoodsNo);
        foreach ($arrSupplierInfo as $key => $value) {
            $objGoodsRealatedSupplierDao->pbGoodsRealatedSupplierInsert($value["SUPPLIER_NO"], $strGoodsNo);
        }
    }

    public function pbGoodsRealatedSupplierInsert($arrSupplierInfo, $strGoodsNo) {
        $objGoodsRealatedSupplierDao = $this->objDaoFactory->createDao(daoConfig::T_EC_M_GOODS_REALATED_SUPPLIER_DAO);
        foreach ($arrSupplierInfo as $key => $value) {
            $objGoodsRealatedSupplierDao->pbGoodsRealatedSupplierInsert($value["SUPPLIER_NO"], $strGoodsNo);
        }
    }
    public function pbGoodsDelete($strGoodsNo) {
        $objGoodsDao = $this->objDaoFactory->createDao(daoConfig::T_EC_M_GOODS_DAO);
        $strDeleteRe = $objGoodsDao->pbGoodsDelete($strGoodsNo);
        if ($strDeleteRe != 0) {
            $objGoodsRealatedDao = $this->objDaoFactory->createDao(daoConfig::T_EC_M_GOODS_REALATED_CATG_DAO);
            $intDeleteRe = $objGoodsRealatedDao->pbDeleteCatgRealated($strGoodsNo);
            $objGoodsRealatedSupplierDao = $this->objDaoFactory->createDao(daoConfig::T_EC_M_GOODS_REALATED_SUPPLIER_DAO);
            $objGoodsRealatedSupplierDao->pbGoodsRealatedSupplierDelete($strGoodsNo);
            return $intDeleteRe;
        } else {
            return $strDeleteRe;
        }
        
    }
}